package org.jeecg.modules.website.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.website.entity.WebSchoolIntroduction;
import org.jeecg.modules.website.entity.WebSchoolMessage;
import org.jeecg.modules.website.service.IWebSchoolIntroductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 学校概况表
* @Author: jeecg-boot
* @Date:   2024-08-02
* @Version: V1.0
*/
@Api(tags="网站API-学校概况表")
@RestController
@RequestMapping("/api/web/webSchoolIntroduction")
@Slf4j
public class WebSchoolIntroductionApiController extends JeecgController<WebSchoolIntroduction, IWebSchoolIntroductionService> {
   @Autowired
   private IWebSchoolIntroductionService webSchoolIntroductionService;

   /**
    * 分页列表查询
    *
    * @param webSchoolIntroduction
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "学校概况表-分页列表查询")
   @ApiOperation(value="学校概况表-分页列表查询", notes="学校概况表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<WebSchoolIntroduction>> queryPageList(WebSchoolIntroduction webSchoolIntroduction,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="12") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<WebSchoolIntroduction> queryWrapper = QueryGenerator.initQueryWrapper(webSchoolIntroduction, req.getParameterMap());
       Page<WebSchoolIntroduction> page = new Page<WebSchoolIntroduction>(pageNo, pageSize);
       queryWrapper.lambda().select(WebSchoolIntroduction.class, i -> !i.getProperty().equals("content"));
       queryWrapper.eq("publish_status",1); // 只查询已发布的
       queryWrapper.last(" order by publish_time desc"); // 根据发布时间排序
       IPage<WebSchoolIntroduction> pageList = webSchoolIntroductionService.page(page, queryWrapper);
       return Result.OK(pageList);
   }


   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   //@AutoLog(value = "学校概况表-通过id查询")
   @ApiOperation(value="学校概况表-通过id查询", notes="学校概况表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<WebSchoolIntroduction> queryById(@RequestParam(name="id",required=true) String id) {
       WebSchoolIntroduction webSchoolIntroduction = webSchoolIntroductionService.getById(id);
       if(webSchoolIntroduction==null) {
           return Result.error("未找到对应数据");
       }
       // 每加载一次 阅读量+1
       webSchoolIntroduction.setReadNum(webSchoolIntroduction.getReadNum()+1);
       webSchoolIntroductionService.updateById(webSchoolIntroduction);
       return Result.OK(webSchoolIntroduction);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param webSchoolIntroduction
   */
   @RequiresPermissions("website:web_school_introduction:exportXls")
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, WebSchoolIntroduction webSchoolIntroduction) {
       return super.exportXls(request, webSchoolIntroduction, WebSchoolIntroduction.class, "学校概况表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequiresPermissions("website:web_school_introduction:importExcel")
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, WebSchoolIntroduction.class);
   }

}
